<?php
/**
 * Контроллер по умолчанию
 * 
 * Дефолтный тонтроллер
 * @author Anton Zavidov
 * @package myLiveShop
 */

/**
 * Дефолтный тонтроллер
 *
 */
class ControllerMain extends Controller{
	/**
	 * Констуктор
	 *
	 * @param string $className имя контроллера
	 */
	public function __construct($className=null){	
		$this->name='mail';
		parent::__construct($className?$className:__CLASS__);
	}
	public function show(){
		$this->view->show(Core::isLogin()?'test':'main_login');
	}
	public function login(){
	    import('helper.autontefication');
		if(HelperAutontefication::login(
				Request::getString('email',''),//,'post'),
				Request::getString('password','')//,'post')
		)){
			if(Core::isJSON()) {
                $this->view->result();
                return true;
            }
			Core::eraseRequest();
			Core::redirect(base64_decode(Request::getString(Core::REFERER)),T::_('Logined'));
		}else {
			if(Core::isJSON()) {
                $this->view->result(T::_('Error_login'));
                return false;
            }
			Core::saveReferer();
			Core::saveRequest();
			Core::redirect(Core::getReferer(true),T::_('Error_login'));
		}
	}
	public function display(){
		$this->show();
	}
	public function logout(){
		import('helper.autontefication');
		HelperAutontefication::logout();
		if(Core::isJSON()) {
            $this->view->result();
            return true;
        }
		Core::redirect(Core::getReferer(true),T::_('Logout_ok'));
	}
	public function info(){
		if(!Core::isJSON()) return false;
		$user_info = array('user_logined'=>Core::isLogin());
		if(Core::isLogin()){
            ///надо переделать, добавить параметр в таблицу пользователей чтоб не вытаскивать каждыцй раз из другой базы
            $user_info['user_shop'] = import('model.shop', true)->getShopsByUser($_SESSION['user_id']);
            $user_info['user_article'] = $this->model('article')->loadCount(array('where'=>ModelArticle::F_USER_ID .'='.$_SESSION['user_id']. " AND ".ModelArticle::F_SHOP_ID."=0"));

			foreach ($_SESSION as $key=>$item) {
				if(strpos($key,'user_')!==false)$user_info[$key]=$_SESSION[$key];
			}
		}
		$this->view->set('user_info', $user_info);
	}
	public function shop(){
		if(!Core::isJSON()) return false;
		$shop = import('model.shop', true);
		$shop_info = $shop->loadOne(Core::shop());
		$shop_info['tags'] = $shop->getAllTags($shop_info[ModelShop::F_SHOP_ID]);
		$shop_info['currency_name'] = $shop_info['currency_id'] ? $this->model('currency')->loadOne($shop_info['currency_id'],array(ModelCurrency::F_NAME)) : '';
		$shop_info['image'] = import('helper.image',true)->get($shop_info[ModelArticle::F_IMAGE_ID]);
		if(Request::getString('mode')=='full'){
			$shop_info['categories'] = import('model.category', true)->load(false,false,array(
				'where'=> ModelShop::F_SHOP_ID.'="'.$shop_info[ModelShop::F_SHOP_ID].'"'
			));
			$shop_info['articles'] = import('model.article', true)->load(false,array(
			  ModelArticle::F_ARTICLE_ID,
			  ModelArticle::F_TITLE,
			  ModelArticle::F_DESCRIPTION,
			  ModelArticle::F_CATEGORY_ID,
			  ModelArticle::F_CREATED,
			  ModelArticle::F_MODEL,
			  ModelArticle::F_IMAGE_ID,
			  ModelArticle::F_SHOP_ID,
			  ModelCategory::TABLE.DOT.ModelCategory::F_TITLE,
			  ModelImage::TABLE.DOT.ModelImage::F_FILENAME
			),
			array(
				'where'=> ModelArticle::TABLE.DOT.ModelArticle::F_SHOP_ID.'="'.$shop_info[ModelShop::F_SHOP_ID].'"'
			));
		}
		$this->view->set('shop_info', $shop_info);
	}
}